package com.example.yoho.mvp.view.fragment;



import android.graphics.Color;



import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;


import com.example.lib_core.mvp.view.BaseFragment;
import com.example.yoho.R;
import com.example.yoho.di.component.DaggerBrandComponent;
import com.example.yoho.di.moudles.BrandModules;
import com.example.yoho.mvp.contract.BrandContract;
import com.example.yoho.mvp.model.entity.BrandGoodsEntity;
import com.example.yoho.mvp.model.entity.BrandListEntity;
import com.example.yoho.mvp.model.entity.BrandTabEntity;
import com.example.yoho.mvp.model.entity.MenuEntity;
import com.example.yoho.mvp.presenter.BrandPresenter;
import com.example.yoho.mvp.view.adapter.MyBrandAdapter;
import com.example.yoho.mvp.view.adapter.MyTabsBrandAdapter;
import com.example.yoho.utils.MyCustom;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout;
import com.jwenfeng.library.pulltorefresh.view.CategoryHeadRefreshView;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import q.rorbin.verticaltablayout.VerticalTabLayout;
import q.rorbin.verticaltablayout.adapter.SimpleTabAdapter;
import q.rorbin.verticaltablayout.widget.ITabView;
import q.rorbin.verticaltablayout.widget.QTabView;
import q.rorbin.verticaltablayout.widget.TabView;

/**
 * A simple {@link Fragment} subclass.
 */
public class BrandFragment extends BaseFragment<BrandPresenter>implements BrandContract.View {


    private com.flyco.tablayout.CommonTabLayout brandComtab;
    private android.support.v7.widget.RecyclerView bradRecyc;
    private List<CustomTabEntity> list_cust=new ArrayList<>();
    private MyBrandAdapter adapter;
    private List<BrandGoodsEntity.ValuesBean> list=new ArrayList<>();
    private com.jwenfeng.library.pulltorefresh.PullToRefreshLayout brandgoodsPull;
    private q.rorbin.verticaltablayout.VerticalTabLayout vertab;
    private List<String> list_tv=new ArrayList<>();
    private List<BrandTabEntity.ValuesBean> list_brandtab=new ArrayList<>();
    private MyTabsBrandAdapter tabs_adapter;
    private TextView textView;
    private View inflate;
    private Handler handler=new Handler();
    private PullToRefreshLayout brandPull;


    @Override
    public int bondLayout() {
        return R.layout.fragment_brand;
    }

    @Override
    public void Inject() {
        DaggerBrandComponent.builder().brandModules(new BrandModules(this)).build().Inject(this);
    }

    @Override
    public void initView() {
        brandComtab = (CommonTabLayout) findviewByIds(R.id.brand_comtab);
        bradRecyc = (RecyclerView) findviewByIds(R.id.brad_recyc);
        vertab = (VerticalTabLayout) findviewByIds(R.id.vertab);
        brandPull = (PullToRefreshLayout) findviewByIds(R.id.brand_pull);
        brandPull.setHeaderView(new CategoryHeadRefreshView(getContext()));
    }

    @Override
    public void initData() {
        mPresenter.BrandRequestAll(1,1);

        adapter=new MyBrandAdapter(R.layout.brandgoods_item,list);
        bradRecyc.setAdapter(adapter);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 3);

        gridLayoutManager.setSpanSizeLookup(new SpanSizeLooksup());
        bradRecyc.setLayoutManager(gridLayoutManager);

        inflate = LayoutInflater.from(getContext()).inflate(R.layout.headview_brandtv, null);
        textView=inflate.findViewById(R.id.tv_brand);
        adapter.addHeaderView(inflate);

        View view = LayoutInflater.from(getContext()).inflate(R.layout.footview_brand, null);

        RecyclerView hotdoorRecv=view.findViewById(R.id.hotdoor_recv);

        vertab.addTab(new QTabView(getActivity()));

        tabs_adapter=new MyTabsBrandAdapter(R.layout.brandgoods_item,list_brandtab);

        hotdoorRecv.setAdapter(tabs_adapter);

        GridLayoutManager tabgridLayoutManager = new GridLayoutManager(getContext(), 3);

        tabgridLayoutManager.setSpanSizeLookup(new SpanSizeLooksup());

        hotdoorRecv.setLayoutManager(tabgridLayoutManager);

        adapter.addFooterView(view);

        brandpulls();

    }

    private void brandpulls() {
        brandPull.setRefreshListener(new BaseRefreshListener() {
            @Override
            public void refresh() {
                list.clear();
                list_cust.clear();
                list_brandtab.clear();
                list_tv.clear();
                mPresenter.BrandRequestAll(1,1);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void loadMore() {

            }
        });
    }


    @Override
        public void showMenu(MenuEntity menuEntity) {
        Log.i("ysk", "showMenu: "+menuEntity.getValues().size());
            List<MenuEntity.Values> values = menuEntity.getValues();
            for (int i = 0; i < values.size(); i++) {
                list_cust.add(new MyCustom(values.get(i).getMenu_name(),0,0));
            }
        brandComtab.setTabData((ArrayList<CustomTabEntity>) list_cust);
    }


    @Override
    public void showBrandTab(final List<BrandTabEntity.ValuesBean> brandTabEntities) {
        Log.e("ysk", "showBrandTab: "+brandTabEntities.size());
        list_brandtab.addAll(brandTabEntities);
        tabs_adapter.notifyDataSetChanged();
        for (int i = 0; i < brandTabEntities.size(); i++) {
            list_tv.add(brandTabEntities.get(i).getCategory_name());
        }
        vertabAll(brandTabEntities);
    }


    private void vertabAll(final List<BrandTabEntity.ValuesBean> brandTabEntities) {
        vertab.addTab(new QTabView(getActivity()));
        vertab.setTabAdapter(new SimpleTabAdapter() {
            @Override
            public int getCount() {
                return list_tv.size();
            }

            @Override
            public ITabView.TabTitle getTitle(int position) {
                ITabView.TabTitle.Builder builder = new ITabView.TabTitle.Builder();
                ITabView.TabTitle tabTitle = builder.setContent(list_tv.get(position))
                        .setTextColor(Color.BLACK, Color.GRAY)
                        .build();
                return tabTitle;
            }
        });
        vertab.addOnTabSelectedListener(new VerticalTabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabView tab, int position) {

                textView.setText("-- 全部" + brandTabEntities.get(position).getCategory_name() + " --");

                final List<BrandTabEntity.ValuesBean> listMakeclear = new ArrayList<>();
                final List<BrandGoodsEntity.ValuesBean> listclear = new ArrayList<>();
                listMakeclear.addAll(list_brandtab);
                listclear.addAll(list);
                list_brandtab.clear();
                list.clear();
                adapter.notifyDataSetChanged();
                tabs_adapter.notifyDataSetChanged();

                Timer timer = new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        list_brandtab.addAll(listMakeclear);
                        list.addAll(listclear);
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                adapter.notifyDataSetChanged();
                                tabs_adapter.notifyDataSetChanged();
                                cancel();
                            }
                        });
                    }
                }, 100, 1000);
            }

            @Override
            public void onTabReselected(TabView tab, int position) {

            }
        });
    }

    @Override
    public void showBrandGoods(List<BrandGoodsEntity.ValuesBean> brandGoodsEntities) {

        List<BrandGoodsEntity.ValuesBean> values = brandGoodsEntities;
        Log.i("ysk", "showBrandGoods: "+values.size() );
        list.addAll(values);
        adapter.notifyDataSetChanged();
        brandPull.finishRefresh();
        brandPull.finishLoadMore();


    }

    @Override
    public void showBrandList(BrandListEntity brandListEntity) {

    }

    class SpanSizeLooksup extends GridLayoutManager.SpanSizeLookup {

        @Override
        public int getSpanSize(int i) {
            if (i==0||i==list.size()+1){
                return 3;
            }else {
                return 1;
            }
        }
    }

}
